using System.Collections;

namespace cuberga.Utils
{
    public class Tour
    {
        private ArrayList _tour = null;

        public override bool Equals(object obj)
        {
            Tour tour = obj as Tour;

            if (tour == null)
                return false;

            if (tour.Length != Length)
                return false;

            for (int i = 0; i < Length; i++)
            {
                if ((int)_tour[i] != (int)tour._tour[i])
                    return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #region Contructors
        public Tour(Tour sourceTour)
        {
            _tour = new ArrayList(sourceTour.ToArray());
        }

        public Tour(int[] sourceArray)
        {
            _tour = new ArrayList(sourceArray);
        }

        public Tour(ArrayList sourceArray)
        {
            _tour = new ArrayList(sourceArray);
        }
        #endregion

        public int Length
        {
            get { return _tour.Count; }
        }

        public int[] ToArray()
        {
            return (int[])_tour.ToArray(typeof(int));
        }

        public void InsertTour(Tour subtour, int iPosition)
        {
            _tour.InsertRange(iPosition, subtour._tour);
        }

        public void AddTour(Tour subtour)
        {
            _tour.AddRange(subtour._tour);
        }

        public Tour Subtour(int iPosition, int iCount)
        {
            ArrayList subtour = new ArrayList(_tour.GetRange(iPosition, iCount));
            return new Tour(subtour);
        }

        public Tour SubtourFromIndex(int iPosition)
        {
            return Subtour(iPosition, Length - iPosition);
        }

        public Tour CutSubtour(int iPosition, int iCount)
        {
            Tour subtour = Subtour(iPosition, iCount);
            _tour.RemoveRange(iPosition, iCount);
            return subtour;
        }

        public int IndexOfCity(int iCity)
        {
            return _tour.IndexOf(iCity);
        }

        public void Swap(int iFirst, int iSecond)
        {
            int iTemp = (int)_tour[iFirst];
            _tour[iFirst] = _tour[iSecond];
            _tour[iSecond] = iTemp;
        }

        public int this[int iIndex]
        {
            get { return (int)_tour[iIndex]; }
            set { _tour[iIndex] = value; }
        }
    }
}